package cn.iocoder.an.module.mold.service.warehouse;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryImportExcelVO;
import cn.iocoder.an.module.mold.dal.dataobject.category.CategoryDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import io.micrometer.common.util.StringUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import cn.iocoder.an.module.mold.controller.admin.warehouse.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.module.mold.dal.mysql.warehouse.WarehouseMapper;
import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.EQUIPMENT_MANAGE_NOT_EXISTS;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具仓库 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Long createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 插入
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        warehouseMapper.insert(warehouse);

        // 返回
        return warehouse.getId();
    }

    @Override
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseExists(updateReqVO.getId());
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Long id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    @Override
    public void deleteWarehouseListByIds(List<Long> ids) {
        // 删除
        warehouseMapper.deleteByIds(ids);
    }


    private void validateWarehouseExists(Long id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(Long id) {
        return warehouseMapper.selectById(id);
    }


    @Override
    public Long getWarehouseIdByCode(String code) {
        WarehouseDO warehouse = warehouseMapper.selectFirstOne(WarehouseDO::getCode, code);
        if (warehouse != null) {
            return warehouse.getId();
        } else {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public List<WarehouseDO> getWarehouseAll() {
        return warehouseMapper.selectList();
    }


    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }


    @Override
    public ImportRespVO importWarehouseList(List<WarehouseImportExcelVO> iomportWarehouse, boolean isUpdateSupport) {
        if (CollUtil.isEmpty(iomportWarehouse)) {
            throw exception(WAREHOUSE_CODE_DUPLICATE);
        }

        ImportRespVO respVO = ImportRespVO.builder().insertNameList(new ArrayList<>())
                .updateNameList(new ArrayList<>()).failureMap(new LinkedHashMap<>()).build();

        AtomicInteger count = new AtomicInteger(1);

        iomportWarehouse.forEach(item -> {
            count.incrementAndGet();//记录行号
            // 2.1.1 校验编码
            if (StringUtils.isEmpty(item.getCode())) {
                return;
            }

            // 2.1.2 校验名称
            if (StringUtils.isEmpty(item.getName())) {
                respVO.getFailureMap().put(item.getCode(), "名称不能为空!");
                return;
            }

            //更新或者插入
            //判断图号是否重复
            String code = item.getCode();
            WarehouseDO exists = warehouseMapper.selectFirstOne(
                    WarehouseDO::getCode, code
            );
            if (exists != null) {
                if (isUpdateSupport) {
                    //允许更新
                    //设置ID
                    item.setId(exists.getId());
                    WarehouseDO warehouseDO = BeanUtils.toBean(item, WarehouseDO.class);
                    warehouseMapper.updateById(warehouseDO);
                    respVO.getUpdateNameList().add(item.getCode());
                } else {
                    respVO.getFailureMap().put(item.getCode(), WAREHOUSE_CODE_DUPLICATE.getMsg());
                }
            } else {
                warehouseMapper.insert(BeanUtils.toBean(item, WarehouseDO.class));
                respVO.getInsertNameList().add(item.getCode());
            }

        });

        return respVO;
    }
}